package org.soretro.portfolio.view.pages
{
	import com.facebook.graph.Facebook;
	import com.greensock.TweenLite;
	
	import flash.events.MouseEvent;
	import flash.net.LocalConnection;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	public class FunStuffMediator extends PagesMediator
	{
		[Inject]
		public var view:FunStuffView;

		private var leftPlaylistId:String;
		private var rightPlaylistId:String;
		
		protected static var APP_ID:String;
		
		override public function onRegister():void
		{
			view.alpha = 0;
			
			view.outputTxt.text = "Checking for login details...";
			facebookTryLogin();
			
			view.leftTitleText.addEventListener( MouseEvent.CLICK, playlistLeftClicked );
			view.rightTitleText.addEventListener( MouseEvent.CLICK, playlistRightClicked );
			
			view.aButtonLogin.addEventListener(MouseEvent.CLICK, facebookLogin );
			view.aButtonLogout.addEventListener(MouseEvent.CLICK, facebookLogout );
			
			TweenLite.to( view, 2, { alpha:1 } );
		}
		
		private function facebookTryLogin():void
		{
			var lc:LocalConnection = new LocalConnection();
			if ( lc.domain.toString() == "localhost" ) {
				APP_ID = "219853668104945"; // Localhost
			} else {
				APP_ID = "178866992214045"; // Live
			}
			
			Facebook.init( APP_ID, onInit );
		}
		
		public function onInit( session:Object, fail:Object ):void {
			if ( session ) {
				view.outputTxt.text = "Checking for login details...";
				Facebook.api("/me", getMeHandler);
				view.aButtonLogin.visible = false;
				view.aButtonLogout.visible = false;
			} else {
				view.outputTxt.text = "You need to login with Facebook to fully use this page.";
				view.aButtonLogin.visible = true;
				view.aButtonLogout.visible = false;
			}
		}
		
		protected function getMeHandler(success:Object,fail:Object):void {
			if (success) {
				view.outputTxt.text = "Hello "+ success.name +",\n";
				view.outputTxt.text += "Thank you for loging into Facebook so I could prove to you that I can develop with the Facebook API.";
				view.fbPhoto.source = "https://graph.facebook.com/"+ success.id +"/picture";
				view.aButtonLogin.visible = false;
				view.aButtonLogout.visible = true;
			}
		}
		
		override public function preRemove():void {
			view.aButtonLogin.removeEventListener(MouseEvent.CLICK, facebookLogin );
			view.aButtonLogout.removeEventListener(MouseEvent.CLICK, facebookLogout );
			TweenLite.to( view, 0.2, { alpha:0 } );
			super.preRemove();
		}
		
		protected function facebookLogin( event:MouseEvent ):void {
			Facebook.login( onInit );
		}
		
		private function onLoginComplete( bool:Boolean ):void
		{
			view.outputTxt.text = "onLoginComplete";
			view.aButtonLogin.visible = false;
			view.aButtonLogout.visible = true;
		}
		
		protected function facebookLogout( event:MouseEvent ):void {
			Facebook.logout( onLogout );
		}
		
		protected function onLogout( bool:Boolean ):void {
			view.aButtonLogin.visible = true;
			view.aButtonLogout.visible = false;
			view.fbPhoto.source = "./assets/fbIconEmpty.jpg";
			view.outputTxt.text = "You need to login with Facebook to fully use this page.";
		}
		
		protected function playlistLeftClicked(event:MouseEvent):void
		{
			//				http://www.youtube.com/playlist?list=PLDBFAAC5D944EB162&feature=viewall
			if ( event.shiftKey ) {
				var url:String = "http://www.youtube.com/playlist?action_edit=1&list=PL"+ leftPlaylistId;
				var request:URLRequest = new URLRequest(url);
				
				try {
					navigateToURL(request, '_youTubeEditor');
				} catch (e:Error) {
					trace("YouTube editor error occurred!");
				}
			}
		}
		
		protected function playlistRightClicked(event:MouseEvent):void
		{
			//				http://www.youtube.com/playlist?list=PLDBFAAC5D944EB162&feature=viewall
			if ( event.shiftKey ) {
				var url:String = "http://www.youtube.com/playlist?action_edit=1&list=PL"+ rightPlaylistId;
				var request:URLRequest = new URLRequest(url);
				
				try {
					navigateToURL(request, '_youTubeEditor');
				} catch (e:Error) {
					trace("YouTube editor error occurred!");
				}
			}
		}
	}
}